import { Response } from 'express';

/**
 * 成功响应
 * @param res - Express response 对象
 * @param data - 响应数据
 * @param message - 响应消息
 * @param status - HTTP状态码
 */
export const success = <T = any>(
  res: Response,
  data: T | null = null,
  message = '操作成功',
  status = 200
): Response => {
  return res.status(status).json({
    success: true,
    message,
    data,
    timestamp: new Date().toISOString()
  });
};

/**
 * 错误响应
 * @param res - Express response 对象
 * @param message - 错误消息
 * @param status - HTTP状态码
 * @param errors - 详细错误信息
 */
export const error = (
  res: Response,
  message = '操作失败',
  status = 500,
  errors: string[] | null = null
): Response => {
  const response: any = {
    success: false,
    message,
    timestamp: new Date().toISOString()
  };

  if (errors) {
    response.errors = errors;
  }

  return res.status(status).json(response);
};

/**
 * 分页响应
 * @param res - Express response 对象
 * @param data - 数据列表
 * @param total - 总数量
 * @param page - 当前页码
 * @param limit - 每页数量
 * @param message - 响应消息
 */
export const paginated = <T = any>(
  res: Response,
  data: T[],
  total: number,
  page = 1,
  limit = 10,
  message = '获取数据成功'
): Response => {
  const totalPages = Math.ceil(total / limit);
  
  return res.json({
    success: true,
    message,
    data,
    pagination: {
      total,
      page: Number(page),
      limit: Number(limit),
      totalPages,
      hasNext: page < totalPages,
      hasPrev: page > 1
    },
    timestamp: new Date().toISOString()
  });
};
